/*
 * Copyright 2017 JessYan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.rlz.image.imageloader;

import android.content.Context;

import com.rlz.image.config.ImageConfig;
import com.rlz.libs.log.P;

import androidx.annotation.Nullable;

/**
 * 泛型
 */
public final class ImageLoader {

    private static volatile ImageLoader sImageLoader;

    public static ImageLoader get() {
        if (sImageLoader == null) {
            synchronized (ImageLoader.class) {
                if (sImageLoader == null) {
                    sImageLoader = new ImageLoader();
                }
            }
        }
        return sImageLoader;
    }


    @Nullable
    BaseImageLoaderStrategy<? extends ImageConfig> mStrategy;

    public ImageLoader() {
    }

    /**
     * 加载图片
     */
    public <T extends ImageConfig> void loadImage(Context context, T config) {
        if (mStrategy == null) {
            P.e("imageLoader", "you must init loader");
            return;
        }
        mStrategy.loadImage(context, config);
    }

    /**
     * 停止加载或清理缓存
     */
    public <T extends ImageConfig> void clear(Context context, T config) {
        if (mStrategy == null) {
            P.e("imageLoader", "you must init loader");
            return;
        }
        mStrategy.clear(context, config);
    }

    /**
     * 可在运行时随意切换 {@link BaseImageLoaderStrategy}
     */
    public void set(BaseImageLoaderStrategy<?> strategy) {
        mStrategy = strategy;
    }

    @Nullable
    public BaseImageLoaderStrategy<?> getLoadImgStrategy() {
        return mStrategy;
    }
}
